var contextPath = $("#contextPath").val();
var context=$("#context");
$(function() {
	dataInit();
});
//升级vip
function upVclick(level,needMoney,oldLevel){
	layer.open({
        content: '确认升级到VIP'+level+'吗？',
        btn: ['确认', '不要'],
        yes: function(){
        	$.ajax({
        		url:contextPath+"/user/myvip/upVip",
        		type:"post",	
				data:{"level":level,"needMoney":needMoney,"oldLevel":oldLevel},
				dataType:"json",			
        		success : function(data, textStatus) {
        			/*layer.open({
        			    content:data.result.message,
        			    time: 2
        			});
        			if(data.result.state=='1'){
        				location.reload(true);
        			}*/
        			location.reload(true);
        		},
        		error : function(XMLHttpRequest, textStatus, errorThrown){
        		}
        	});
        } 
      });
}
//充值升级vip
function rechargeVip(balance,price,level){
	  $("#curBalance").text('当前余额:'+balance+'元');
	  $("#needMoney").text(price);
	  $("#zhifubao").attr('href',contextPath+'/vipUpRecharge/confirm_alipay_recharge?price='+price+'&vipLevel='+level);
	  $("#weixin").attr('href','javascript:wxzf('+price+','+level+');');
	  init();
}
function  wxzf(price,level){
	$.ajax({
		url:contextPath+'/vipUpRecharge/confirm_wxjs_recharge',
		type:"post",
		async:true,
		data:{"price":price,"vipLevel":level},
		dataType:"json",
		success : function(data, textStatus) {
			var state = data.result.state;
			console.log(data);
			if(state == 1){
				doWxJsPay(data.prePayInfo);
			}else{
				layer.open({
				    title: '提示',
				    content: data.result.message,
				    btn: ['我知道了']
				});
			}
			layer.close(load);
		},
		error : function(XMLHttpRequest, textStatus, errorThrown){
		}
	});	
}
function doWxJsPay(prePayInfo){
  	if (typeof WeixinJSBridge == "undefined"){
  		   if( document.addEventListener ){
  		       document.addEventListener('WeixinJSBridgeReady', function(){
  		    	   onBridgeReady(prePayInfo);
  		       }, false);
  		   }else if (document.attachEvent){
  		       document.attachEvent('WeixinJSBridgeReady', function(){
  		    	   onBridgeReady(prePayInfo);
  		       }); 
  		       document.attachEvent('onWeixinJSBridgeReady', function(){
  		    	   onBridgeReady(prePayInfo);
  		       });
  		   }
  		}else{
  		   onBridgeReady(prePayInfo);
  		}
  }
  function onBridgeReady(prePayInfo){
  	WeixinJSBridge.invoke('getBrandWCPayRequest',prePayInfo,
  		    function(res){
  		       //支付成功或失败前台判断
      	       if(res.err_msg=='get_brand_wcpay_request:ok'){
      	    	   layer.open({
  					    title: '提示',
  					    content: '恭喜你，升级成功！',
  					    btn: ['我知道了'],
  					    yes: function(index){
  					        layer.close(index);
  					        location.reload(true);
  					    }
  					});	
      	       }else if(res.err_msg=='get_brand_wcpay_request:cancel'){	   
      	    	   layer.open({
  					    title: '提示',
  					    content: '支付已取消！',
  					    btn: ['我知道了']
  					});	
      	       }else{
      	    	   layer.open({
  					    title: '提示',
  					    content: '支付失败，请重试！',
  					    btn: ['我知道了']
  					});	
      	       }
  		     });
  	
  }
function explain(level,price){
	$.ajax({
		url:contextPath+"/user/myvip/Explain",
		type:"post",	
		data:{"level":level},
		dataType:"json",			
		success : function(data, textStatus) {
			var dataMessage=data.result.message;
			$(".directions_txt").text(dataMessage.substring(0,dataMessage.length-3)+price+dataMessage.substring(dataMessage.length-3,dataMessage.length));
		},
		error : function(XMLHttpRequest, textStatus, errorThrown){
		}
	});
}
//数据的显示
function dataInit(){
	$.ajax({
		url:contextPath+"/myvip/upgrade",
		type:"post",
		async:true,
		dataType:"json",
		success : function(data, textStatus) {
		var balance=data.userAccount.balance;//账户余额
		//var vipAddPrice=data.userAccount.vipAddPrice;//vip累计消费金额
		var userLevel=data.level;//用户当前vip等级
		//var saveMoney=0;//用户可以升级减去钱数
        $.each(data.vipAllLevel,function(i,v){
        	var vipcontext='<div class="pay_mode_item" onClick="explain('+v.level+','+v.price+');"><div class="pay_mode_conter"><p class="c_bright"><span class="dib vm vip__vipgrade">'+
			'<img src="'+contextPath+'/static/images/icon_vipgrade_0'+v.level+'.png" alt=""></span> 会员</p>';
        	//如果用户vip等级大于等于此等级，则显示已升级
        	if(v.level<=userLevel){
        		vipcontext+='<a class="btn_vip_grade btn_vipup_no" href="javascript:;">已升级</a></div></div>';
        	}/*else if(v.level==userLevel&&vipAddPrice>=v.price){
        		vipcontext+='<a class="btn_vip_grade btn_vipup_no" href="javascript:;">已升级</a></div></div>';
        		saveMoney=v.price;
        	}else if(v.level==userLevel&&vipAddPrice<v.price){
        		vipcontext+='<a class="btn_vip_grade btn_vipup_no" href="javascript:;">已升级</a></div></div>';
        		saveMoney=vipAddPrice;
        	}*/
        	//如果用户账户余额大于等于此等级下vip升级所需钱数，则直接升级vip,否则就用升级vip所需钱数减去用户账户余额则是升级所需充值钱数
        	else if(balance>=v.price){
        		vipcontext+='<a class="btn_vip_grade btn_vipup" href="javascript:upVclick('+v.level+','+v.price+','+userLevel+');">升级</a></div></div>';
        	}else{
        		var price=(v.price-balance).toFixed(3);//升级需要充值的钱
        		price=price.substring(0,price.length-1);
        		vipcontext+='<a class="btn_vip_grade btn_vipup" href="javascript:rechargeVip('+balance+','+price+','+v.level+');">升级</a></div></div>';
        	}
        	context.append(vipcontext);
        	$('.explanation').html('<div class="directions">说明：<div class="directions_txt"> 请升级VIP或充值！</div></div>');
		});
		},
		error : function(XMLHttpRequest, textStatus, errorThrown){
		}
	});		
}